package com.caocaog.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caocaog.market.entity.GoodsAttr;
import com.caocaog.market.entity.GoodsBasic;
import com.caocaog.market.entity.GoodsSpec;
import com.caocaog.market.mapper.GoodsBasicMapper;
import com.caocaog.market.service.*;
import com.caocaog.market.vo.GoodsAttributeVo;
import com.caocaog.market.vo.GoodsVo;
import com.google.common.base.Joiner;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 商品表
 * @Author: jeecg-boot
 * @Date: 2021-11-12
 * @Version: V1.0
 */
@Service
public class GoodsBasicServiceImpl extends ServiceImpl<GoodsBasicMapper, GoodsBasic> implements IGoodsBasicService {

    @Autowired
    private GoodsAttrService goodsAttrService;
    @Autowired
    private GoodsSpecService goodsSpecService;

    /*@Autowired
    private IGoodsRelationService goodsRelationService;*/

    @Autowired
    private GoodsHisService goodsHisService;
    /*@Resource
    private GoodsSortInfoMapper goodsSortInfoMapper;*/

    @Override
    public IPage<GoodsBasic> queryGoodsBasicPage(Page<GoodsBasic> page, GoodsBasic goodsBasic) {
        String sorts = goodsBasic.getSorts();
        if (StringUtils.isNotBlank(sorts) && sorts.contains("*")) {
            goodsBasic.setSorts(sorts.substring(1, sorts.length() - 1));
        }
        return super.baseMapper.queryGoodsBasicPage(page, goodsBasic);
    }

    @Override
    public Result add(GoodsBasic goodsBasic) {
        List<GoodsAttributeVo> goodsAttributeVoList = goodsBasic.getGoodsAttributeVoList();

        List<GoodsAttributeVo> goodsSpecificationsVoList = goodsBasic.getGoodsSpecificationsVoList();
        //最小价格
        BigDecimal minPrice = new BigDecimal("0");
        BigDecimal price = new BigDecimal(goodsBasic.getPresentPrice());
        for (GoodsAttributeVo vo : goodsSpecificationsVoList) {
            BigDecimal p = new BigDecimal(vo.getPriceSku()).setScale(2, BigDecimal.ROUND_UP);
            vo.setPriceSku(p.toString());
            if (price.compareTo(p) >= 0 || goodsSpecificationsVoList.size() == 1) {
                minPrice = p;
            }
        }
        goodsBasic.setPresentPrice(String.format("%.2f", minPrice));
        if (StringUtils.isNotBlank(goodsBasic.getDescription())) {
            goodsBasic.setShortDescription(goodsBasic.getDescription());
        }
        if (StringUtils.isNotBlank(goodsBasic.getShortDescription())) {
            goodsBasic.setDescription(goodsBasic.getShortDescription());
        }

        this.baseMapper.insert(goodsBasic);
        String goodsId = goodsBasic.getId();
        if (oConvertUtils.isEmpty(goodsBasic.getCode())) {
            goodsBasic.setCode(goodsId);
        }
        this.baseMapper.updateById(goodsBasic);
        List<GoodsAttr> goodsAttrList = new ArrayList<>();
        for (GoodsAttributeVo vo : goodsAttributeVoList) {
            GoodsAttr goodsAttr = new GoodsAttr();
            goodsAttr.setName(vo.getNameSku());
            goodsAttr.setGoodsId(goodsId);
            goodsAttr.setValue(Joiner.on(",").join(vo.getValueSku()));
            goodsAttrService.insertGoodsAttr(goodsAttr);
            goodsAttrList.add(goodsAttr);
        }
        List<GoodsSpec> goodsSpecList = new ArrayList<>();
        for (GoodsAttributeVo vo : goodsSpecificationsVoList) {
            GoodsSpec goodsSpec = new GoodsSpec();
            goodsSpec.setName(vo.getNameSku());
            goodsSpec.setGoodsId(goodsId);
            goodsSpec.setPackFee(vo.getPackFeeSku());
            // TODO 小程序选择规格界面的图片不再是规格的Picture时就把这行删了
            goodsSpec.setPicture(goodsBasic.getPicture());
            goodsSpec.setPrice(vo.getPriceSku());
            goodsSpec.setStock(vo.getStockSku());
            goodsSpec.setWeight(vo.getWeightSku());
            goodsSpec.setWeightG(vo.getWeightGSku());
            goodsSpecService.insertGoodsSpec(goodsSpec);
            goodsSpecList.add(goodsSpec);
        }
        //表关联
        /*GoodsRelation relation = new GoodsRelation();
        relation.setShopId("1");
        relation.setGoodsId(goodsId);
        goodsRelationService.save(relation);*/
        //表分类
        //分类
        /*String[] sortsStr = goodsBasic.getSorts().split(",");
        for (String s : sortsStr) {
            GoodsSortInfo goodsSort = new GoodsSortInfo();
            goodsSort.setSortId(s);
            goodsSort.setGoodsId(goodsId);
            goodsSortInfoMapper.insert(goodsSort);
        }*/
        // 历史
//        goodsHisService.insertGoodsHis(goodsBasic, true, goodsAttrList, goodsSpecList);
        return Result.OK("添加成功", true);
    }

    @Override
    public Result update(GoodsBasic goodsBasic) {

        List<GoodsAttributeVo> goodsAttributeVoList = goodsBasic.getGoodsAttributeVoList();

        List<GoodsAttributeVo> goodsSpecificationsVoList = goodsBasic.getGoodsSpecificationsVoList();
        //最小价格
        BigDecimal minPrice = new BigDecimal("0");
        BigDecimal price = new BigDecimal(goodsBasic.getPresentPrice());
        for (GoodsAttributeVo vo : goodsSpecificationsVoList) {
            System.out.println(vo.getId());
            BigDecimal p = new BigDecimal(vo.getPriceSku()).setScale(2, BigDecimal.ROUND_UP);
            vo.setPriceSku(p.toString());
            if (price.compareTo(p) >= 0 || goodsSpecificationsVoList.size() == 1) {
                minPrice = p;
            }
        }
        goodsBasic.setPresentPrice(minPrice.toString());
        this.baseMapper.updateById(goodsBasic);
        String goodsId = goodsBasic.getId();
        goodsAttrService.deleteGoodsAttrByGoodsId(goodsId);
        goodsSpecService.deleteGoodsSpecByGoodsId(goodsId);
        List<GoodsAttr> goodsAttrList = new ArrayList<>();
        for (GoodsAttributeVo vo : goodsAttributeVoList) {
            GoodsAttr goodsAttr = new GoodsAttr();
            goodsAttr.setName(vo.getNameSku());
            goodsAttr.setGoodsId(goodsId);
            goodsAttr.setValue(Joiner.on(",").join(vo.getValueSku()));
            goodsAttrService.insertGoodsAttr(goodsAttr);
            goodsAttrList.add(goodsAttr);
        }
        List<GoodsSpec> goodsSpecList = new ArrayList<>();
        for (GoodsAttributeVo vo : goodsSpecificationsVoList) {
            GoodsSpec goodsSpec = new GoodsSpec();
            goodsSpec.setName(vo.getNameSku());
            goodsSpec.setGoodsId(goodsId);
            goodsSpec.setPackFee(vo.getPackFeeSku());
            // TODO 小程序选择规格界面的图片不再是规格的Picture时就把这行删了
            goodsSpec.setPicture(goodsBasic.getPicture());
            goodsSpec.setPrice(vo.getPriceSku());
            goodsSpec.setStock(vo.getStockSku());
            goodsSpec.setWeight(vo.getWeightSku());
            goodsSpec.setWeightG(vo.getWeightGSku());
            goodsSpecService.insertGoodsSpec(goodsSpec);
            goodsSpecList.add(goodsSpec);
        }
        //分类
        /*String[] sortsStr = goodsBasic.getSorts().split(",");
        goodsSortInfoMapper.delete(new QueryWrapper<GoodsSortInfo>().eq("goods_id", goodsId));
        for (String s : sortsStr) {
            GoodsSortInfo goodsSortInfo = new GoodsSortInfo();
            goodsSortInfo.setSortId(s);
            goodsSortInfo.setGoodsId(goodsId);
            goodsSortInfoMapper.insert(goodsSortInfo);
        }*/
        // 历史
//        goodsHisService.insertGoodsHis(goodsBasic, false, goodsAttrList, goodsSpecList);
        return Result.OK("修改成功", true);
    }

    @Override
    public GoodsVo getGoodsInfo(String goodsId) {
        GoodsBasic goodsBasic = super.baseMapper.selectById(goodsId);
        List<GoodsAttr> attributeList = goodsAttrService.getGoodsAttrByGoodsId(goodsId);
        List<GoodsSpec> specificationsList = goodsSpecService.getGoodsSpecByGoodsId(goodsId);
        GoodsVo vo = new GoodsVo();
        BeanUtils.copyProperties(goodsBasic, vo);
        vo.setGoodsAttributeList(attributeList);
        vo.setGoodsSpecificationsList(specificationsList);
        goodsBasic.setVisitNum(goodsBasic.getVisitNum() + 1);
        super.baseMapper.updateById(goodsBasic);
        return vo;
    }

    @Override
    public GoodsVo getGoodsInfoForSDX(String goodsId) {
        GoodsBasic goodsBasic = super.baseMapper.selectById(goodsId);
        List<GoodsAttr> attributeList = goodsAttrService.getGoodsAttrByGoodsId(goodsId);
        List<GoodsSpec> specificationsList = goodsSpecService.getGoodsSpecByGoodsId(goodsId);
        GoodsVo vo = new GoodsVo();
        BeanUtils.copyProperties(goodsBasic, vo);
        vo.setGoodsAttributeList(attributeList);
        vo.setGoodsSpecificationsList(specificationsList);
        goodsBasic.setVisitNum(goodsBasic.getVisitNum() + 1);
        super.baseMapper.updateById(goodsBasic);
        return vo;
    }

    @Override
    public GoodsVo getGoodsInfo(GoodsVo goodsVo) {
        GoodsBasic goodsBasic = super.baseMapper.selectById(goodsVo.getId());
        List<GoodsAttr> attributeList = goodsAttrService.getGoodsAttrByGoodsId(goodsVo.getId());
        List<GoodsSpec> specificationsList = goodsSpecService.getGoodsSpecByGoodsId(goodsVo.getId());

        goodsVo.setGoodsAttributeList(attributeList);
        goodsVo.setGoodsSpecificationsList(specificationsList);
        goodsVo.setVisitNum(goodsBasic.getVisitNum() + 1);
        super.baseMapper.updateById(goodsBasic);
        return goodsVo;
    }

    @Override
    public List<GoodsVo> getShopOfGoodsPushList(String shopId,Integer limit) {
        List<GoodsVo> goodsVoList = new ArrayList<>();
        List<GoodsBasic>  goodsBasicList = super.baseMapper.selectList(new QueryWrapper<GoodsBasic>()
                .eq("shop_id",shopId)
                .eq("status",1)
                .eq("del_flag",0)
                .orderByDesc("is_hot")
                .orderByDesc("sale_num")
                .last(" limit "+limit+" "));
        for (GoodsBasic basic: goodsBasicList){
            GoodsVo goodsVo = new GoodsVo();
            BeanUtils.copyProperties(basic,goodsVo);
            goodsVoList.add(goodsVo);
        }
        return goodsVoList;
    }
}
